//
//  QLOAuthViewController.swift
//  WeiBo
//
//  Created by 黄跃奇 on 16/9/21.
//  Copyright © 2016年 itheima. All rights reserved.
//  https://api.weibo.com/oauth2/authorize?client_id=2905697524&redirect_uri=http://www.oschina.net

import UIKit
//第三方登录视图控制器
//新浪开发平台提供的APPKey
let weiBoAPPKey = "2905697524"
//授权回调页
let weiBoRedirect_Uri = "http://www.oschina.net"

class QLOAuthViewController: UIViewController {
    
    //MARK: - 懒加载
    private lazy var webView: UIWebView = UIWebView()
    
    //重写loadView，目的是把webView作为视图控制器的view
    override func loadView() {
        //改成透明解决底部webView黑条
        webView.isOpaque = false
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        requestLogin()
        
        setupNavUI()
    }

    //请求第三方登录
    private func requestLogin() {
        //url
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(weiBoAPPKey)&redirect_uri=\(weiBoRedirect_Uri)"
        //创建urlRequest对象
        let urlRequest = URLRequest(url: URL(string: url)!)
        webView.loadRequest(urlRequest)
    }
    
    //设置导航栏按钮
    private func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: #selector(cancelAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: #selector(autoFillAction))
        navigationItem.title = "新浪微博"
    }
    
    //MARK: -- 点击事件
    @objc func cancelAction() {
        dismiss(animated: true, completion: nil)
    }

    @objc func autoFillAction() {
        print("自动填充")
        webView.stringByEvaluatingJavaScript(from: "document.getElementById('userId').value = 'huangyueqi1101@163.com';document.getElementById('passwd').value = 'ddddd'")
    }
}













